const express = require("express");
var MongoClient = require("mongodb").MongoClient;
const router = express.Router();
const { ObjectId } = require("mongodb");
const uri = "mongodb://127.0.0.1:27017";
const client = new MongoClient(uri);

router.post("/action/dynamicLoadInfo", async (req, res) => {
  var obj = req.body;
  let dataObj = {};
  let subcommand = obj.para1.split(" ")[1];
  console.log(subcommand);
  try {
    await client.connect();
    const sdtclis = client.db("homeIndex").collection("sdtclis");
    await sdtclis.find(obj).toArray();

    if (subcommand == "getstatus") {
      dataObj.devstatus = "成功！";
    } else if (subcommand == "listuser") {
      dataObj.stickList = [
        {
          name: "admin",
          status: "在线",
        },
        {
          name: "audit",
          status: "在线",
        },
        {
          name: "test",
          status: "在线",
        },
      ];
    } else {
    }
    dataObj.result = true;
    dataObj.errno = 0;
    dataObj.errmsg = "ok";
    res.send(dataObj);
  } catch (err) {
    dataObj.result = false;
    dataObj.errno = -1;
    dataObj.errmsg = "失败！";
    res.send(dataObj);
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }

  // cliAll.sdtclis.find(req, (err, result) => {
  //   if (!err) {
  //     if (subcommand == "getstatus") {
  //       dataObj.devstatus = "成功！";
  //     } else if (subcommand == "listuser") {
  //       dataObj.stickList = [
  //         {
  //           name: "admin",
  //           status: "在线",
  //         },
  //         {
  //           name: "audit",
  //           status: "在线",
  //         },
  //         {
  //           name: "test",
  //           status: "在线",
  //         },
  //       ];
  //     } else {
  //     }
  //     dataObj.result = true;
  //     dataObj.errno = 0;
  //     dataObj.errmsg = "ok";
  //     res.send(dataObj);
  //   } else {
  //     dataObj.result = false;
  //     dataObj.errno = -1;
  //     dataObj.errmsg = "失败！";
  //     res.send(dataObj);
  //   }
  // });
});
module.exports = router;
